﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AliCMS.TemplateEngine
{
    internal class Code : Tag
    {
        internal Code(Engine engine, int index, int length, int contentIndex, int contentLength, Tag parent)
            : base(engine, index, length, contentIndex, contentLength, parent)
        {
        }

        internal override void Parse()
        {
            int index = Parent == null ? Index : ContentIndex;
            int length = Parent == null ? Length : ContentLength;
            StringBuilder result = new StringBuilder(Engine.Text.Substring(index, length));
            for (int i = 0; i < ChildTags.Count; i++)
            {
                Tag tag = ChildTags[i];
                result.Remove(tag.Index - index, tag.Length);
                result.Insert(tag.Index - index, tag.ResultCode);
            }

            ResultCode = result.ToString();
        }
    }
}
